//
//  SingleImgViewController.swift
//  Status
//
//  Created by 花轮 on 15/5/21.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit

// 处理单张图片的显示
class SingleImgViewController: UIViewController {
    
    var scale: CGFloat = 1
    
    var imageURL: NSURL? {
        didSet{
            imageView.sd_setImageWithURL(imageURL, placeholderImage: nil) { (_, _, _, _) in
                self.imageView.sizeToFit()
            }
        }
    }
    
    lazy var imageView: UIImageView = {
        let iv = UIImageView()
        
        // 添加手势
        let pinch = UIPinchGestureRecognizer(target: self, action: "pinch:")
        iv.addGestureRecognizer(pinch)
        
        iv.userInteractionEnabled = true
        
        return iv
    }()
    
    
    override func loadView() {
        view = UIView(frame: UIScreen.mainScreen().bounds)
        view.backgroundColor = UIColor.clearColor()
        view.addSubview(imageView)
    }
    func pinch(gesture: UIPinchGestureRecognizer) {
//        println(gesture.scale)
        scale = gesture.scale
        
        if gesture.state == UIGestureRecognizerState.Changed
        {
            // 建立形变矩阵
            let transform = CGAffineTransformMakeScale(scale, scale)
            
            if scale < 1.0
            {
                // 开始交互式转场
                startInteractiveTransition(self)
            }
            else{
                
                imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale)
            }
        }
        else if gesture.state == UIGestureRecognizerState.Ended || gesture.state == UIGestureRecognizerState.Cancelled
        {
            if scale < 0.5
            {
                completeTransition(true)
            }
        }
        
    }
}

extension SingleImgViewController: UIViewControllerInteractiveTransitioning {
    func startInteractiveTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 将目标视图控制器的视图，添加到容器视图中
        let toViewVc = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        let toView = toViewVc!.view
        
        let dummyV = toView.snapshotViewAfterScreenUpdates(false)
        transitionContext.containerView().insertSubview(dummyV, belowSubview: view)
        
        view.alpha = scale
        view.transform = CGAffineTransformMakeScale(scale, scale)
    }
}

extension SingleImgViewController: UIViewControllerContextTransitioning {
    // 转场的容器视图
    func containerView() -> UIView{
        println(__FUNCTION__)
        return view.superview!
    }
    
    // 是否转场结束
    func completeTransition(didComplete: Bool) {
        
        println(__FUNCTION__)

        dismissViewControllerAnimated(true, completion: nil)
    }
    
    // 取参与转场的视图控制器 - 要提供转场到哪里的控制器
    func viewControllerForKey(key: String) -> UIViewController?
    {
        println(__FUNCTION__)

        if key == UITransitionContextToViewKey
        {
            return (UIApplication.sharedApplication().keyWindow!.rootViewController!.childViewControllers[0] as! UINavigationController).topViewController
        }
        else {
            return self
        }
    }
    
        // 是否在交互
        func isInteractive() -> Bool {
            println(__FUNCTION__)

            return true
        }
        // 可以不用的函数
        func isAnimated() -> Bool{
            println(__FUNCTION__)

            return true
        }
        // 是否转场结束
        
        func transitionWasCancelled() -> Bool {
            println(__FUNCTION__)

            return false
        }
        
        func presentationStyle() -> UIModalPresentationStyle{
            println(__FUNCTION__)

            return UIModalPresentationStyle.Custom
        }
        
        // of a present or dismiss, the transitioningDelegate.
        func updateInteractiveTransition(percentComplete: CGFloat) {}
        func finishInteractiveTransition() {}
        func cancelInteractiveTransition() {}
    
    
    
    func viewForKey(key: String) -> UIView? {
        println(__FUNCTION__)

        return nil
    }
    
    func targetTransform() -> CGAffineTransform {
        println(__FUNCTION__)

        return CGAffineTransformIdentity
    }
   
    func initialFrameForViewController(vc: UIViewController) -> CGRect
    {
        println(__FUNCTION__)

        return CGRectZero
    }
    func finalFrameForViewController(vc: UIViewController) -> CGRect
    {
        println(__FUNCTION__)

        return CGRectZero
    }
}
